//
//  UITableViewAdditions.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "UITableViewAdditions.h"


@implementation UITableView (Tint)


#pragma mark - Querying

- (UITableViewCell *)dequeueReusableCellWithClass:(Class)cls
{
    if ( cls ) {
        
        NSString *identifier = NSStringFromClass(cls);
        UITableViewCell *cell = nil;
        
        cell = [self dequeueReusableCellWithIdentifier:identifier];
        
        if ( cell == nil ) {
            cell = [[[cls alloc] init] autorelease]; // AUTO
        }
        
        return cell;
    }
    
    return [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease]; // AUTO
}

- (UITableViewCell *)firstRowInSection:(NSUInteger)section
{
    if ( section < [self numberOfSections] ) {
        NSUInteger rows = [self numberOfRowsInSection:section];
        if ( rows > 0 ) {
            return [self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
        }
    }
    return nil;
}

- (UITableViewCell *)lastRowInSection:(NSUInteger)section
{
    if ( section < [self numberOfSections] ) {
        NSUInteger rows = [self numberOfRowsInSection:section];
        if ( rows > 0 ) {
            return [self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rows-1 inSection:section]];
        }
    }
    return nil;
}



#pragma mark - Refreshing

- (void)refreshSectionsWithRowFadeAnimation
{
    if ( [self numberOfSections] > 0 ) {
        [self reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSections])] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)refreshSectionWithRowFadeAnimation:(NSUInteger)section
{
    if ( section < [self numberOfSections] ) {
        [self reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
    }
}

@end
